// 
// $Rev:: 22            $:  Revision of last commit
// $Author:: rvhaverm@g#$:  Author of last commit
// $Date:: 2010-07-25 1#$:  Date of last commit
// 
package agents.agents;

import com.ibm.aglet.*;

import java.util.Hashtable;

import agents.aglets.AgletBridge;
import agents.configuration.IHostConfiguration;
import agents.interfaces.IMessage;


/**
 * A type of agent that can help agents that need certain types of information,
 * to find eachother.
 * 
 * This agent serves as a receptionist for the host it lives in. It contains a
 * hashtable of services, that maps strings onto AgletProxies. Each String, that
 * serves as a key in the hashtable, has a certain type of service associated
 * with it, following the Conventions in the Travel Agent System. These
 * conventions are:
 * 
 * <p>
 * <ul>
 * <li>"TourismManager" -> TourismManager</li>
 * <li>"HotelManager" -> HotelManager</li>
 * <li>"TCManager" -> TransportCompanyManager</li>
 * <li>"DBAgent" -> DBAgent</li>
 * </ul>
 * </p>
 * 
 * With this design, agents that require some service just have to send the
 * Receptionist a message containing the right (String) key.
 * 
 * @author Ruben Van Havermaet
 * @version 0.2
 */
public class Receptionist extends QueryAgent
{

    /**
     * A Hashtable containing the currently available ServiceAgents that live
     * within the host of the Receptionist.
     */
    protected Hashtable _services = null;

    // /** Properties of the host for which this Receptionist is working. */
    // protected HostProperties _props = null;

    private IHostConfiguration _configuration = null;

    /**
     * An Initialising method that is called when the agent is created.
     * 
     * Next to calling the superclass' onCreation method, this method makes sure
     * that the agent responsible for the User Interface, uiAgent_, is properly
     * set.
     * 
     * @param init
     *            an Object that must be used to pass the uiAgent AgletProxy.
     */
    public void onCreation(Object init)
    {
	super.onCreation(init);

	// Initialise the Services hashtable
	_services = new Hashtable();
    }

    

    // *** PUBLIC MESSAGE METHODS *********************************************

    /**
     * Returns the system properties to the sender of the Message.
     * 
     * @param msg
     *            the Message
     * @see agents.msgs.GetSystemPropertiesMessage
     */
    public void getSystemProperties(Message msg)
    {
	// AgletBridge.sendReply(msg, _props);
    }

    /**
     * Let other agents register their services with the Receptionist.
     * 
     * This method handles messages from other agents that want to register
     * their services with the Receptionist, so that the rest of the world can
     * ask the Receptionist where they can find every service.
     * 
     * @param msg
     *            a Message
     * @see agents.msgs.RegisterServiceMessage
     */
    public void registerService(String serviceName, AgletProxy agent)
    {
	// Enter the data in the services_ hashtable.
	_services.put(serviceName, agent);
    }

    /**
     * Let other agents be able to ask the Receptionist for AgletProxies that
     * provide certain services.
     * 
     * This method extracts the String from msg, determining the type of service
     * requested. It then looks up the corresponding AgletProxy in its
     * hashtable, services_.
     * 
     * @param msg
     * @see agents.msgs.GetServiceMessage
     */
    public void getService(Message msg)
    {
	String label = (String) msg.getArg();
	// displayText("returning the requested service: " + label);
	AgletBridge.sendReply(msg, _services.get(label));
    }

    public void setSystemProperties(IHostConfiguration configuration)
    {
	_configuration = configuration;
    }

    @Override
    public void receive(IMessage message)
    {
	message.sentTo(this);
    }
}
